package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.GateWayDto;
import com.c611.smartclassroom.mapper.GateWayMapper;
import com.c611.smartclassroom.model.Build;
import com.c611.smartclassroom.model.Device;
import com.c611.smartclassroom.model.GateWay;
import com.c611.smartclassroom.service.DeviceService;
import com.c611.smartclassroom.service.GateWayService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("gateway")
public class GateWayController {

    @Autowired
    GateWayService gateWayService;
    @Autowired
    DeviceService deviceService;
    @Autowired
    GateWayMapper gateWayMapper;

    /**
     * 根据教学楼id和查询信息，查询所有网关
     * @param buildId 这个参数必须有
     * @param status 可不填
     * @param gateWayId 网关id或网关Mac可不填
     * @return
     */
    @PostMapping("queryGateWay")
    public DataGridView<GateWayDto> queryGateWay(@RequestParam("buildId") String buildId, @RequestParam(value = "status",required = false,defaultValue = "-1")
            int status, @RequestParam(value = "gateWayId",required = false) String gateWayId){
        if(buildId == null) return null;
        List<GateWayDto> gateWayList = gateWayService.queryGateWayByBuildId(buildId,status,gateWayId);
        return new DataGridView<GateWayDto>(0,"ok",(long)(gateWayList.size()),gateWayList);
    }
    /**
     * 根据点击的教学楼Id和学校，查询所有网关
     * @param buildId
     * @return
     */
    /*@PostMapping("queryGateWayByBuildId")
    public List<GateWay> queryGateWayByBuildId(String buildId) {
        if (buildId != null){
            return gateWayService.queryGateWayByBuildId(buildId);
        }
        return null;
    }*/
    /**
     * 根据网关ID查询该网关下的所有设备，用于查看网关的设备信息
     * @param gateWayId
     * @return 该网关下的所有设备
     */
    @PostMapping("queryDeviceByGateId")
    public List<Device> queryDeviceByGateId(String gateWayId){
        if (gateWayId == null) return null;

        return deviceService.queryDeviceByGateId(gateWayId);
    }
    /**
     * 通过教室Id查询网关
     * @param classroomId
     * @return
     */
    @PostMapping("queryGateWayByClassroomId")
    public List<GateWay> queryGateWayByClassroomId(String classroomId) {
        if (classroomId == null) return null;
        return gateWayService.queryGateWayByClassroomId(classroomId);
    }

    /**
     * 增加网关
     * @param gateWay
     * @return 返回增加结果状态
     */
    @PostMapping("addGateWay")
    public int addGateWay(GateWay gateWay,String tbuildid) {
        //确认不能为空的字段有数据
        if (gateWay == null || gateWay.getClassroomid() == null ||
                gateWay.getGatewaymac() == null || gateWay.getGatewayid() == null)
            return 0;
        //查询是否存在该网关id
        if (gateWayService.queryGateWayByGateWayId(gateWay.getGatewayid()) != null)
            return 2;
        //返回是否添加成功的值
        int flag = gateWayService.addGateWay(gateWay);
        //添加buildId
        if (flag == 1) gateWayMapper.addTbuildId(tbuildid,gateWay.getGatewayid());
        return flag;
    }
    /**
     * 编辑网关
     * @param gateWay
     * @return
     */
    @PostMapping("saveGateWay")
    public int saveGateWay(GateWay gateWay,String tbuildid) {
        //判断参数是否为空
        if (gateWay == null ) return 0;

        //查询是否存在该网关
        GateWay gateWay1 = gateWayService.queryGateWayByGateWayId(gateWay.getGatewayid());
        if (gateWay1 == null) return 0;
        int flag = gateWayService.saveGateWay(gateWay);
        if (flag == 1 && tbuildid != null) gateWayMapper.addTbuildId(tbuildid,gateWay.getGatewayid());
        return flag;
    }

    /**
     * 删除网关
     * @param gateWayId
     * @return 返回删除结果状态
     */
    @PostMapping("delGateWay")
    public int delGateWay(String gateWayId) {
        if (gateWayId == null) return 0;
        //查询是否存在该id的网关
        GateWay gateWay = gateWayService.queryGateWayByGateWayId(gateWayId);
        if (gateWay == null) return 0;

        return gateWayService.delGateWay(gateWayId);
    }

}
